package cn.fighter3.service.node;

import cn.fighter3.entity.node.People;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Optional;

public interface PeopleDao extends Neo4jRepository<People,String> {


    //所有cypher语句查询
    @Query(value="match $cypher")
    List<Object> findByCypher(@Param("cypher") String cypher);


    List<People> findByAuthorName(String name);
    //增加
//    People create(People people);

    //删除
    void deleteByAuthorId(String id);

    //@Query("match (n:People) return n limit 25")
    @Query("match (a:People)<-[r]-(b) return a,count(r) order by count(r) DESC limit 20")
    List<People> all();

    @Query("match (n:People) where n.authorName=$name detach delete n")
    void deleteByAuthorName(String name);

    //查询
    Optional<People> findByAuthorId(String id);
    //模糊查询
//    @Query("match(n:People) where n.authorName =~'.*{name}.*' return n")
//    People find(@Param(value="name") String name);
    List<People> findByAuthorNameLike(String name);


    //更新修改
//    @Query("MATCH (n) WHERE n.authorId = :#{#people.authorId} SET n.authorName = :#{#people.authorName} RETURN n")
//    People updateByNode(@Param("people") People people);
    @Query("MATCH (n:People) WHERE n.authorId = $id SET n.authorName = $name RETURN n")
    People updateByNode(String id,String name);

    //由书找人:Authorof
    @Query("match (a)-[r:Authorof]->(c:People) WHERE a.bookId= $fromid RETURN c")
    List<People> findByfromid(String fromid);

    //由书找人--Lend
    @Query("match (a:People)-[r:Lend]->(c) WHERE c.bookId= $toid RETURN a")
    List<People> findByfromid11(String toid);

    //查询关系数量
    //MATCH (n:People {authorId:$id})-[]-() RETURN count(*)
    //profile match (n:People) where n.authorId = $id return size((n)-[]-())
    @Query("MATCH (n:People {authorId:$id})-[]-() RETURN count(*)")
    Integer countpeo(String id);




}
